package my.diplom.web.controllers;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.validation.BindException;
import my.diplom.dao.service.CategoryService;
import my.diplom.dao.service.StatusService;
import my.diplom.entity.Category;
import my.diplom.entity.Status;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.beans.PropertyEditorSupport;

/**
 * Created by IntelliJ IDEA.
 * User: GGobozov
 * Date: 28.07.2009
 * Time: 16:36:34
 * To change this template use File | Settings | File Templates.
 */
public class CategoryController extends SimpleFormController {

    private CategoryService categoryService;
    private StatusService statusService;


    public CategoryController(){
        setCommandClass(Category.class);
        setCommandName("newCategory");
        setSessionForm(false);
        setBindOnNewForm(false);
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public void setStatusService(StatusService statusService) {
        this.statusService = statusService;
    }


     @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        if (request.getParameter("categoryId") != null && request.getParameter("action").equals("edit")) {
            Category category = categoryService.getById(Long.parseLong(request.getParameter("categoryId")));
            return category;
        }  else {
            Category category = new Category();
            return category;
        }
    }

     @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        List<Category> categories = categoryService.getAll();
        HashMap<String, Object> model = new HashMap<String, Object>();
        model.put("cats", categories);
        return model;
    }


     @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o, BindException e) throws Exception {
        Category category = (Category) o;
        categoryService.create(category);
        return new ModelAndView(new RedirectView("createCategory.htm"));
    }

     @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
          binder.registerCustomEditor(Status.class, "status", new PropertyEditorSupport() {

            public void setAsText(String text) {
                if (text instanceof String) {
                    if (text.equals("inactive")) {
                        Status status = statusService.getByName("inactive");
                        setValue(status);
                    } else if (text.equals("active")) {
                        Status status = statusService.getByName("active");
                        setValue(status);
                    }

                }
            }

            public String getAsText() {
                Object value = getValue();
                if (value != null) {
                    Status status = (Status) value;
                    return status.getStatusName();
                }
                return null;
            }


        });

     }

}
